package com.hg.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hg.common.HttpStatus;
import com.hg.common.R;
import com.hg.mapper.HgMallDishMapper;
import com.hg.mapper.HgMallShopMapper;
import com.hg.pojo.HgMallDish;
import com.hg.pojo.HgMallShop;
import com.hg.pojo.HgUser;
import com.hg.service.HgMallShopService;
import com.hg.service.HgUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.File;
import java.util.List;

@Service
public class HgMallShopServiceImpl extends ServiceImpl<HgMallShopMapper, HgMallShop> implements HgMallShopService {

    @Autowired
    private HgMallShopMapper hgMallShopMapper;

    @Autowired
    private HgMallDishMapper hgMallDishMapper;

    @Autowired
    private HgUserService hgUserService;
    @Override
    @Transactional
    public int deleteShopById(Integer id) {
        if (id == null) {
            return 0;
        }

        LambdaUpdateWrapper<HgMallShop> lmU = new LambdaUpdateWrapper<>();
        lmU.set(HgMallShop::getDelFlag, 1);
        lmU.eq(HgMallShop::getId, id);
        int update = hgMallShopMapper.update(null, lmU);
        if (update > 0) {
            LambdaUpdateWrapper<HgMallDish> lmDish = new LambdaUpdateWrapper<>();
            lmDish.set(HgMallDish::getDelFlag, 1);
            lmDish.eq(HgMallDish::getShopId, id);
            hgMallDishMapper.update(null, lmDish);
            LambdaUpdateWrapper<HgUser> lmUser=new LambdaUpdateWrapper<>();
            lmUser.set(HgUser::getShopId,null);
            lmUser.eq(HgUser::getShopId,id);
            hgUserService.update(lmUser);
            return 1;
        }
        return 0;
    }

    @Override
    public int updateShop(HgMallShop hgMallShop) {

        return hgMallShopMapper.updateById(hgMallShop);
    }

    @Override
    public int deleteShopByIds(Integer[] ids) {
        LambdaUpdateWrapper<HgMallShop> lmU = new LambdaUpdateWrapper<>();
        lmU.set(HgMallShop::getDelFlag, 1);
        lmU.in(HgMallShop::getId, ids);
        int update = hgMallShopMapper.update(null, lmU);
        if (update>0){
            LambdaUpdateWrapper<HgMallDish> lmDish=new LambdaUpdateWrapper<>();
            lmDish.set(HgMallDish::getDelFlag, 1);
            lmDish.in(HgMallDish::getId, ids);
            hgMallDishMapper.update(null,lmDish);
        }
        return 0;
    }

    @Override
    @Transactional
    public int MyShopAddOne(Integer id, HgMallShop hgMallShop) {
        if (id==null){
            return 0;
        }
        HgUser one = hgUserService.getById(id);
        if (one.getShopId()==null){
            boolean save = this.save(hgMallShop);
            LambdaUpdateWrapper<HgUser> lmUpdate=new LambdaUpdateWrapper<>();
            lmUpdate.set(HgUser::getShopId,hgMallShop.getId());
            lmUpdate.eq(HgUser::getId,id);
            hgUserService.update(lmUpdate);
            if (save){
                return 1;
            }
        }
        return 0;
    }
}
